define(["jquery","bbcare"],function($,$bb){
    var servers = {};
    var URL = $bb.baseUrl("nutrition");

    /**
     * 获取上一个页面传过来的参数
     */
    servers.getPageParams = function(){
        var params = $bb.getPageParams();
        return params;
    }

    /**
     * 删除疗程库
     */
    servers.ajaxCourseLibirayDel = function(params){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/courselibrary/delete",
            data: JSON.stringify({
                data: { 
                    "courseLibraryId":params.libraryId 
                }
            }),
            success: function (data) {
                if(data.success){
                    params.callback(data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除疗程库出错！");
                }
            }
        });
    }

    /**
     * 查询疗程库
     */
    servers.ajaxCourseLibiray = function(callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/courselibiray/list",
            success: function (data) {
                if(data.success){
                    callback(data.rows);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询疗程库出错！");
                }
            }
        });
    };

    /**
     * 根据疗程库ID、疗程类型ID，查询疗程库菜单
     */
    servers.ajaxCourseMenus = function(libraryId,callback,req){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/course/menulist",
            data: JSON.stringify({
                data: { 
                    "libraryId":libraryId 
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data,req);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询疗程库菜单出错！");
                }
            }
        });
    };

    /**
     * 处理疗程库菜单数据
     */
    servers.handleCourseContent = function(data){
        var flag = false;
        if(data.length > 0){
            for (var i = 0; i < data.length; i++) {
                if( data[i].isLeaf == "1" ){
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    /**
     * 疗程类型添加或修改
     */
    servers.ajaxCourseTypeModify = function(params,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/coursetype/modify",
            data: JSON.stringify({
                data:params
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                    var str = params.courseTypeId ? "修改" : "新添";
                    $bb.toastShow(data.msg ? data.msg : str + "疗程类型出错！");
                }
            }
        });
    }

    /**
     * 疗程类型删除
     */
    servers.ajaxCourseTypeDel = function(params,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/courseTypeDel",
            data: JSON.stringify({
                data:params
            }),
            success: function (data) {
                if(data.success){
                    callback(data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除疗程类型出错！");
                }
            }
        });
    }
    /**
     * 疗程类型拷贝
     */
    servers.ajaxCourseClone = function(params){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/courseClone",
            data: JSON.stringify({
                data:params
            }),
            success: function (data) {
                if(data.success){
                	params.callBack(data.data,params.libraryId,params.className);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "拷贝疗程类型出错！");
                }
            }
        });
    }
    
    /**
     * 添加或修改疗程
     */
    servers.ajaxCourseModify = function(params,callback,libraryId,className){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/coursebasic/modify",
            data: JSON.stringify({
                data:params
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data,libraryId,className);
                }else{
                    var str = params.courseId ? "修改" : "新添";
                    $bb.toastShow(data.msg ? data.msg : str + "疗程类型出错！");
                }
            }
        });
    }

    /**
     * 删除疗程
     */
    servers.ajaxCourseDel = function(params){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/course/delete",
            data: JSON.stringify({
                data:{
                    "courseId":params.params
                }
            }),
            success: function (data) {
                if(data.success){
                    params.callBack(data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除疗程出错！");
                }
            }
        });
    }

    /**
     * 疗程基本信息查询
     */
    servers.ajaxCoursebasic = function(refId,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/coursebasic/detail",
            data: JSON.stringify({
                data:{
                    "courseId": refId
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "疗程基本信息查询出错！");
                }
            }
        });
    }

    /**
     * 食物库查询
     */
    servers.ajaxFoodlibrary = function(callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/foodlibrary/list",
            success: function (data) {
                if(data.success){
                    callback(data.rows);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "食物库查询出错！");
                }
            }
        });
    }

    /**
     * 食物树形菜单查询
     */
    servers.ajaxFoodMenus = function(foodLibraryId,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/foodtype/menulist",
            data: JSON.stringify({
                data: { 
                    "foodLibraryId":foodLibraryId 
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "食物树形菜单查询出错！");
                }
            }
        });
    };

    /**
     * 疗程营养元素标准详情查询
     */
    servers.ajaxCourseNutrients = function(courseVersionId,callback,flag){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/coursenutrients/detail",
            data: JSON.stringify({
                data: { 
                    "courseVersionId":courseVersionId 
                }
            }),
            success: function(data){
                if(data.success){
                    callback(data.data,courseVersionId,flag);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "疗程营养元素查询出错！");
                }
            }
        });
    }

    /**
     * 查询食物营养成分字典列表
     */
    servers.ajaxNutrientDictQuery = function(courseVersionId,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/nutrientDictQuery",
            success: function(data){
                if(data.success){
                    callback(data.data,courseVersionId);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询食物营养成分字典出错！");
                }
            }
        });
    }

    /**
     * 请求添加疗程营养元素
     */
    servers.ajaxNutrienTelementModify = function(params,callback,className){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/course/nutrientelement/modify",
            data: JSON.stringify({
                data: params
            }),
            success: function(data){
                if(data.success){
                    callback(data.data,params.courseVersionId,className);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "添加疗程营养元素出错！");
                }
            }
        });
    }

    /**
     * 请求删除疗程营养元素
     */
    servers.ajaxNutrienTelementDel = function(params){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/coursenutrients/delete",
            data: JSON.stringify({
                data: {
                    "courseNutrientId":params.courseNutrientId
                }
            }),
            success: function(data){
                if(data.success){
                    params.callback(data,params.courseVersionId);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除疗程营养元素出错！");
                }
            }
        });
    }

    /**
     * 疗程食物查询
     */
    servers.ajaxCoursefood = function(courseVersionId,callback,list,flag){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/coursefood/list",
            data: JSON.stringify({
                data: {
                    "courseVersionId":courseVersionId
                }
            }),
            success: function(data){
                if(data.success){
                    callback(data.data,list,courseVersionId,flag);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "疗程食物查询出错！");
                }
            }
        });
    }

    /**
     * 处理数据（添加时段时需要的）
     */
    servers.handleAddCoursePeriod = function(list){
        var hours = [];
        var min = [];
        for (var i = 0; i <= 24; i++) {
            hours.push(i);
        }
        for (var i = 0; i <= 5; i++) {
            var num = i*10;
            min.push(num);
        }
        for (var i = 0; i < list.length; i++) {
            list[i].startTimeHours = Math.floor( list[i].startTime / 60 );
            list[i].startTimeMin = list[i].startTime % 60;
            list[i].endTimeHours = Math.floor( list[i].endTime  / 60 );
            list[i].endTimeMin = list[i].endTime % 60;
        }
        if(list.length == 0) list.push({});
        return {
            "hours":hours,
            "min":min,
            "list":list
        };
    }

    /**
     * 处理数据（添加时段时需要的）
     */
    servers.handleAddCoursePeriodLi = function(){
        var hours = [];
        var min = [];
        for (var i = 0; i <= 24; i++) {
            hours.push(i);
        }
        for (var i = 0; i <= 5; i++) {
            var num = i*10;
            min.push(num);
        }
        return {
            "hours":hours,
            "min":min
        };
    }

    /**
     * 疗程时段添加/修改
     */
    servers.ajaxCoursePeriodModify = function(params,courseVersionId,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition//courseperiod/modify",
            data: JSON.stringify({
                data: params
            }),
            success: function(data){
                if(data.success){
                    callback(data,courseVersionId);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "保存疗程时段出错！");
                }
            }
        });
    }

    /**
     * 小于10补零
     */
    var fillZero = function(num){
        return num < 10 ? "0" + num : num;
    } 

    /**
     * 处理时段数据
     */
    servers.handleCoursePeriod = function(list,courseVersionId){
        for (var i = 0; i < list.length; i++) {
            var startTime = Math.floor(list[i].startTime / 60) + ':' + fillZero(list[i].startTime % 60);
            var endTime = Math.floor(list[i].endTime / 60) + ':' + fillZero(list[i].endTime % 60);
            list[i].time = startTime + "-" + endTime;
            if( list[i].foodList ){
            }else{
                list[i].foodList = [{}];
            }
        }
        return {"list":list,"courseVersionId":courseVersionId};
    }

    /**
     * 请求删除时段
     */
    servers.ajaxCoursePeriodRemove = function(periodId,courseVersionId,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition//courseperiod/remove",
            data: JSON.stringify({
                data: {
                    "periodId":periodId
                }
            }),
            success: function(data){
                if(data.success){
                    callback(data,courseVersionId);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除时段出错！");
                }
            }
        });
    }

    /**
     * 疗程某时段食物增加、修改 包括食物的食用量
     */
    servers.ajaxCourseFoodModify = function(params,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/coursefood/modify",
            data: JSON.stringify({
                data: params
            }),
            success: function(data){
                if(data.success){
                    callback(data,params.courseVersionId);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "疗程某时段食物增加、修改出错！");
                }
            }
        });
    }

    /**
     * 删除疗程时段中的食物
     */
    servers.ajaxCourseFoodDel = function(courseFoodId,courseVersionId,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/coursefood/delete",
            data: JSON.stringify({
                data: {
                    "courseFoodId":courseFoodId
                }
            }),
            success: function(data){
                if(data.success){
                    callback(data,courseVersionId);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除疗程时段中的食物出错！");
                }
            }
        });
    }

    /**
     * 获取nodeId
     */
    servers.getNodeId = function(data,req){
        if(req.courseId){
            for (var i = 0; i < data.length; i++) {
                if( data[i].refId == req.courseId ){
                    return data[i].id;
                }
            } 
        }else if(req.courseTypeId){
            return req.courseTypeId;
        }
    }

    return servers;
});